<?php

namespace Cktadmin\Controller;

use Think\Controller;
use Common\Service\DataService;

class IndexController extends Controller {

    public function index() {
        (($user_info = session('user_info')) == false) && $this->redirect('/' . MODULE_NAME . '/login');
        $this->display();
    }

    public function main() {
        $this->title = '首页';
        cookie('menu_id', 0);
        if (function_exists("gd_info")) {
            $gd = gd_info();
        }
        $this->sysinfo = array(
            'frame_version' => 'ThinkPHP' . THINK_VERSION, //框架版本号
            'os' => $_SERVER["SERVER_SOFTWARE"], //获取服务器标识的字串
            'version' => PHP_VERSION, //获取PHP服务器版本
            'ip' => $_SERVER['REMOTE_ADDR'], //服务器ip
            'web_root' => $_SERVER['DOCUMENT_ROOT'], //网站目录
            'time' => date("Y-m-d H:i:s", time()), //获取服务器时间
            'pc' => http_type() . $_SERVER['SERVER_NAME'], //当前主机名
            'osname' => php_uname(), //获取系统类型及版本号
            'language' => $_SERVER['HTTP_ACCEPT_LANGUAGE'], //获取服务器语言
            'port' => $_SERVER['SERVER_PORT'], //获取服务器Web端口
            'gd_version' => $gd['GD Version'], //获取服务器GD库版本
            'max_upload' => ini_get("file_uploads") ? ini_get("upload_max_filesize") : "Disabled", //最大上传
            'post_max' => ini_get("post_max_size") ? ini_get("post_max_size") : "Disabled", //最大上传
            'max_ex_time' => ini_get("max_execution_time") . "秒", //脚本最大执行时间
            'mysql_version' => $this->_mysql_version(), //数据库版本
            'mysql_size' => $this->_mysql_db_size(), //数据库大小
        );
        $this->display();
    }

    private function _mysql_version() {
        $version = M()->query("select version() as ver");
        return $version[0]['ver'];
    }

    private function _mysql_db_size() {
        $sql = "SHOW TABLE STATUS FROM " . C('DB_NAME');
        $tblPrefix = C('DB_PREFIX'); //表前缀
        if ($tblPrefix != null) {
            $sql .= " LIKE '{$tblPrefix}%'";
        }
        $row = M()->query($sql);
        $size = 0;
        foreach ($row as $value) {
            $size += $value["data_length"] + $value["index_length"];
        }
        return round(($size / 1048576), 2) . 'M';
    }

    public function pass() {
        if (IS_POST) {
            $username = I('post.username', '', 'trim');
            $oldPwd = I('post.oldPwd', '', 'trim');
            $newPwd = I('post.newPwd', '', 'trim');
            $rePwd = I('post.rePwd', '', 'trim');
            $user_info = D('User')->findUserByName($username);
            $user_info['password'] != encryption($oldPwd . $username) && $this->error('原始密码错误！');
            $newPwd != $rePwd && $this->error('两次输入密码不一致');
            $data['password'] = encryption($newPwd . $username);
            $data['id'] = session('user_info.id');
            if (false !== (DataService::save(D('User'), $data))) {
                session('user_info', null);
                $this->success('修改成功', U('/' . MODULE_NAME . '/login'));
            }
            $this->error('修改失败，请稍候再试');
        } else {
            $this->display();
        }
    }

    /**
     * 删除缓存文件夹
     */
    private function _deleteDir($R) {
        $handle = opendir($R);
        while (($item = readdir($handle)) !== false) {
            if ($item != '.' and $item != '..') {
                if (is_dir($R . '/' . $item)) {
                    $this->_deleteDir($R . '/' . $item);
                } else {
                    if (!unlink($R . '/' . $item))
                        die('error!');
                }
            }
        }
        closedir($handle);
        return rmdir($R);
    }

    /**
     * 清除缓存
     */
    public function clearRuntime() {
        $R = $_GET['path'] ? $_GET['path'] : RUNTIME_PATH;
        if ($this->_deleteDir($R))
            $this->success('清除缓存成功！');
    }

}
